package com.netx.data;
import java.io.IOException;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;


public class FileSink implements DataSink {

	private final File _file;
	
	public FileSink(String filename) {
		if(filename == null) {
			throw new IllegalArgumentException("null filename");
		}
		_file = new File(filename);
		if(!_file.exists()) {
			throw new IllegalArgumentException(filename+": file does not exist");
		}
		else if(_file.isDirectory()) {
			throw new IllegalArgumentException(filename+": is a directory");
		}
	}

	public void update(String query) {
		try {
			PrintWriter writer = new PrintWriter(new FileWriter(_file));
			writer.println(query);
			writer.close();
		}
		catch(IOException io) {
			//TODO have a special log file for this?
			System.out.println(io);
		}
	}

}
